package org.zhq.controller.center;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.zhq.ServerResult;
import org.zhq.pojo.Users;
import org.zhq.pojo.bo.center.CenterUserBO;
import org.zhq.pojo.vo.UsersVO;
import org.zhq.resource.FileUpload;
import org.zhq.service.UsersService;
import org.zhq.service.center.CenterUsersService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Api(value = "用户信息接口", tags = {"用户信息接口"})
@RestController
@RequestMapping("userInfo")
public class CenterUserController {

    private final CenterUsersService centerUsersService;

    private final UsersService usersService;

    private final FileUpload fileUpload;

    @Autowired
    public CenterUserController(CenterUsersService centerUsersService, UsersService usersService, FileUpload fileUpload) {
        this.centerUsersService = centerUsersService;
        this.usersService = usersService;
        this.fileUpload = fileUpload;
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息", httpMethod = "POST")
    @PostMapping("/update")
    public ServerResult update(@RequestParam String userId, @RequestBody CenterUserBO centerUserBO,
                               HttpServletRequest request, HttpServletResponse response) {
        Users users = centerUsersService.updateUserInfo(userId, centerUserBO);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users, usersVO);
        usersService.storeUserForLogin(usersVO, request, response);
        return ServerResult.ok(users);
    }


    @ApiOperation(value = "上传头像", notes = "上传头像", httpMethod = "POST")
    @PostMapping("/uploadFace")
    public ServerResult face(@RequestParam String userId, MultipartFile file,
                             HttpServletRequest request, HttpServletResponse response) throws IOException {
        String fileSpace = fileUpload.getImageUserFaceLocation();
        String uploadPathPrefix = File.separator + userId;
        if (file != null) {
            String originalFilename = file.getOriginalFilename();
            if (StrUtil.isNotBlank(originalFilename)) {
                String[] fileNameArr = originalFilename.split("\\.");
                String suffix = fileNameArr[fileNameArr.length - 1];

                if(!suffix.equalsIgnoreCase("png")
                        && !suffix.equalsIgnoreCase("jpeg")
                        && !suffix.equalsIgnoreCase("jpg")){
                    return ServerResult.errorMsg("图片格式不正确");
                }
                String type = FileTypeUtil.getType(file.getInputStream());
                String newFileName = "face-" + userId + "." + type;
                String targetPath = fileSpace + uploadPathPrefix + File.separator + newFileName;
                File outFile = new File(targetPath);
                if(outFile.getParentFile()!=null){
                    outFile.getParentFile().mkdirs();
                }

                FileOutputStream fileOutputStream = new FileOutputStream(outFile);
                InputStream inputStream = file.getInputStream();
                IoUtil.copy(inputStream,fileOutputStream);
                //todo 更新头像的地址到数据库,由于浏览器存在缓存,加上时间戳  xxxx?t=201901237979
            }
        } else {
            return ServerResult.errorMsg("文件不能为空");
        }
        return ServerResult.ok();
    }

}
